package com.ruoyi.web.controller.api.vipOrder;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PVipOrderBo;
import com.ruoyi.system.domain.vo.PVipOrderVo;
import com.ruoyi.system.service.IPVipOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 会员订单Controller
 *
 * @author ruoyi
 * @date 2022-09-02
 */
@Validated
@Api(value = "会员订单控制器", tags = {"会员订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/vipOrder")
public class PVipOrderController extends BaseController {

    private final IPVipOrderService iPVipOrderService;

    /**
     * 查询会员订单列表
     */
    @ApiOperation("查询会员订单列表")
    @SaCheckPermission("system:vipOrder:list")
    @GetMapping("/list")
    public TableDataInfo<PVipOrderVo> list(PVipOrderBo bo, PageQuery pageQuery) {
        return iPVipOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员订单列表
     */
    @ApiOperation("导出会员订单列表")
    @SaCheckPermission("system:vipOrder:export")
    @Log(title = "会员订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PVipOrderBo bo, HttpServletResponse response) {
        List<PVipOrderVo> list = iPVipOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员订单", PVipOrderVo.class, response);
    }

    /**
     * 获取会员订单详细信息
     */
    @ApiOperation("获取会员订单详细信息")
    @SaCheckPermission("system:vipOrder:query")
    @GetMapping("/{vipOrderId}")
    public R<PVipOrderVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("vipOrderId") Long vipOrderId) {
        return R.ok(iPVipOrderService.queryById(vipOrderId));
    }

    /**
     * 新增会员订单
     */
    @ApiOperation("新增会员订单")
    @SaCheckPermission("system:vipOrder:add")
    @Log(title = "会员订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PVipOrderBo bo) {
        return toAjax(iPVipOrderService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改会员订单
     */
    @ApiOperation("修改会员订单")
    @SaCheckPermission("system:vipOrder:edit")
    @Log(title = "会员订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PVipOrderBo bo) {
        return toAjax(iPVipOrderService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除会员订单
     */
    @ApiOperation("删除会员订单")
    @SaCheckPermission("system:vipOrder:remove")
    @Log(title = "会员订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vipOrderIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] vipOrderIds) {
        return toAjax(iPVipOrderService.deleteWithValidByIds(Arrays.asList(vipOrderIds), true) ? 1 : 0);
    }
}
